package com.foreveross.proxyip.monitor.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.foreveross.proxyip.entity.ExtractLogEntity;
import com.foreveross.proxyip.infra.InstanceFactory;
import com.foreveross.proxyip.infra.utils.StringUtils;
import com.foreveross.proxyip.monitor.infra.SourceRecordRepository;
import com.foreveross.proxyip.monitor.vo.ExtractLogEntityVO;

/**
 * 源IP的领域对象
 * @author Administrator
 *
 */
public class SourceRecord {
	public final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static SourceRecordRepository repository;
	private static SourceRecordRepository getRepository(){
		if(repository==null){
			repository=InstanceFactory.getInstance(SourceRecordRepository.class);
		}
		return repository;
	}
	
	
	public static List<ExtractLogEntity> searchByConditions(ExtractLogEntityVO vo,int pageIndex,int pageLen)
			throws Exception{
		StringBuffer hql=new StringBuffer("select e from ExtractLogEntity e where 1=1");
		List<Object> params=new ArrayList<Object>();
		Object[] obj=generateHqlByConditions(hql, params, vo);
		hql=(StringBuffer)obj[0];
		params=(List<Object>)obj[1];
		//检索时间
		hql.append(" order by e.createTime desc");
		return getRepository().findByHQL(hql.toString(), params.toArray(),pageIndex,pageLen);
	}
	
	
	public static long searchSizeByConditions(ExtractLogEntityVO vo)throws Exception{
		StringBuffer hql=new StringBuffer("select count(e) from ExtractLogEntity e where 1=1");
		List<Object> params=new ArrayList<Object>();
		Object[] obj=generateHqlByConditions(hql, params, vo);
		hql=(StringBuffer)obj[0];
		params=(List<Object>)obj[1];
		return getRepository().findSizeByHQL(hql.toString(), params.toArray());
	}
	
	

	/**
	 * 根据条件拼出通用的查询语句和拼出条件参数
	 * [0]==HQL
	 * [1]==params
	 * @throws Exception 
	 */
	private static Object[] generateHqlByConditions(StringBuffer hql,List<Object> params,ExtractLogEntityVO vo) throws Exception{
		Object[] finalResult=new Object[2];
		if(vo!=null){
			
		    if(!StringUtils.isEmpty(vo.getId())){
					hql.append(" and e.id=?");
					params.add(vo.getId());
			}
		    //这里的机制是，如果指定了createTime,则不再去判断begin 和end ,如果没有指定，则再次去判断
		    if(!StringUtils.isEmpty(vo.getCreateTimeStr())){
		    	hql.append(" and e.createTime=?");
				params.add(sdf.parse(vo.getCreateTimeStr()));
				
		    }else if(!StringUtils.isEmpty(vo.getBeginTimeStr()) && !StringUtils.isEmpty(vo.getEndTimeStr())){
		    	hql.append(" and e.createTime between ? and ?");
		    	params.add(sdf.parse(vo.getBeginTimeStr()));
		    	params.add(sdf.parse(vo.getEndTimeStr()));
		    }else if(!StringUtils.isEmpty(vo.getBeginTimeStr())){
		    	hql.append(" and e.createTime >= ?");
		    	params.add(sdf.parse(vo.getBeginTimeStr()));
		    }else if(!StringUtils.isEmpty(vo.getEndTimeStr())){
		    	hql.append(" and e.createTime <= ?");
		    	params.add(sdf.parse(vo.getEndTimeStr()));
		    }
			
		    if(!StringUtils.isEmpty(vo.getExpendTimeStr())){//毫秒
		    	hql.append(" and e.expendTime<=?");
		    	params.add(Integer.parseInt(vo.getExpendTimeStr()));
		    }
		    if(!StringUtils.isEmpty(vo.getExtractMode())){//IP提供商
		    	hql.append(" and e.extractMode=?");
		    	params.add(vo.getExtractMode());
		    }
		    if(!StringUtils.isEmpty(vo.getExtractNumStr())){
		    	hql.append(" and e.extractNum>=?");
		    	params.add(Integer.parseInt(vo.getExtractNumStr()));
		    }
		    if(!StringUtils.isEmpty(vo.getStateStr())){
		    	hql.append(" and e.state=?");
		    	params.add(Integer.parseInt(vo.getStateStr()));
		    }
		}
		finalResult[0]=hql;
		finalResult[1]=params;
		return finalResult;
	}
}
